require 'active_record/fixtures'

fixture = "data"
directory = "#{Rails.root}/db/#{fixture}"

def yaml_to_database(fixture, path)
  ActiveRecord::Base.establish_connection(Rails.env)
  tables = Dir.new(path).entries.select {|e| e =~ /(.+)?\.yml/}.collect {|c| c.split('.').first}
  ActiveRecord::Fixtures.create_fixtures(path, tables)
end

yaml_to_database(fixture, directory)

#require 'faker'
#
#begin
#  2.times {
#    Address.create!(
#      :street => "#{Forgery(:address).street_address}",
#      :city   => "#{Forgery(:address).city}",
#      :zip    => "#{Forgery(:basic).number :at_most => 99999, :at_least => 11111}"
#    )
#  }
#
#  2.times {
#    date = Forgery(:date).date :future => true
    
#    Order.create!(
#      :number => "#{Forgery(:basic).number}",
#      :total  => "#{Forgery(:monetary).money :min => 3000, :max => 5000}",
#      :item_total => "#{Forgery(:monetary).money :min => 2000, :max => 3000}",
#      :user_info_id => 1,
#
#      :user_name => "#{Forgery(:name).first_name}",
#      :user_surname => "#{Forgery(:name).last_name}",
#      :user_email => "#{Forgery(:internet).email_address}",
#      :user_phone => "#{Forgery(:address).phone}",
#
#      :state => "completed",
#      :completed_at => date,
#      :created_at => date
#    )
#  }
#rescue ActiveRecord::RecordInvalid => invalid
#  invalid.record.errors.full_messages.each do |message|
#    puts "#{message}"
#  end
#end